package tr.edu.metu.is.mega.client.course.student;

import java.util.List;

import tr.edu.metu.is.mega.client.core.AsyncCallbackAdapter;
import tr.edu.metu.is.mega.client.core.DialogCallbackAdapter;
import tr.edu.metu.is.mega.client.core.MainWindow;
import tr.edu.metu.is.mega.client.core.event.SelectionEventBus;
import tr.edu.metu.is.mega.client.core.event.SelectionEventListener;
import tr.edu.metu.is.mega.client.course.CourseChangeEvent;
import tr.edu.metu.is.mega.client.course.activity.ActivityManagementPanel;
import tr.edu.metu.is.mega.client.course.activity.SubmissionReviewDialog;
import tr.edu.metu.is.mega.client.course.announcement.AnnouncementManagementPanel;
import tr.edu.metu.is.mega.client.course.lecturenote.LectureNoteManagementPanel;
import tr.edu.metu.is.mega.client.resource.MEGAResources;
import tr.edu.metu.is.mega.client.service.GwtCourseService;
import tr.edu.metu.is.mega.client.service.GwtCourseServiceAsync;
import tr.edu.metu.is.mega.common.dto.CourseDTO;
import tr.edu.metu.is.mega.common.dto.TeamDTO;
import tr.edu.metu.is.mega.common.dto.UserRole;

import com.extjs.gxt.ui.client.event.ButtonEvent;
import com.extjs.gxt.ui.client.event.SelectionListener;
import com.extjs.gxt.ui.client.util.Margins;
import com.extjs.gxt.ui.client.widget.ContentPanel;
import com.extjs.gxt.ui.client.widget.LayoutContainer;
import com.extjs.gxt.ui.client.widget.button.Button;
import com.extjs.gxt.ui.client.widget.layout.ColumnData;
import com.extjs.gxt.ui.client.widget.layout.ColumnLayout;
import com.extjs.gxt.ui.client.widget.layout.FillData;
import com.extjs.gxt.ui.client.widget.layout.FillLayout;
import com.extjs.gxt.ui.client.widget.layout.RowData;
import com.extjs.gxt.ui.client.widget.layout.RowLayout;
import com.extjs.gxt.ui.client.widget.toolbar.ToolBar;
import com.google.gwt.user.client.Element;
import com.google.gwt.user.client.History;
import com.google.gwt.user.client.ui.AbstractImagePrototype;

/**
 * Main window view of course for students.
 */
public class StudentCourseWindow extends MainWindow {

    private CourseDTO course;

    private TeamDTO team;

    /**
     * Default constructor.
     */
    public StudentCourseWindow() {
        super("StudentCourseWindow", null);
        setHeading("Course Information");
    }

    /**
     * {@inheritDoc}
     */
    @Override
    protected void onRender(Element parent, int pos) {
        super.onRender(parent, pos);
        setLayout(new FillLayout());
        setSize(1024, 768);
        ContentPanel panel = new ContentPanel();
        panel.setLayout(new RowLayout());
        panel.setHeaderVisible(false);
        panel.setBodyBorder(false);

        LayoutContainer topContainer = new LayoutContainer(new ColumnLayout());

        TeamInformationPanel teamInformationPanel = new TeamInformationPanel();
        teamInformationPanel.setStyleAttribute("paddingRight", "4px");
        topContainer.add(teamInformationPanel, new ColumnData(0.4));

        LectureNoteManagementPanel lectureNoteManagementPanel = new LectureNoteManagementPanel(UserRole.STUDENT);
        lectureNoteManagementPanel.setStyleAttribute("paddingRight", "4px");
        topContainer.add(lectureNoteManagementPanel, new ColumnData(0.3));

        AnnouncementManagementPanel announcementManagementPanel = new AnnouncementManagementPanel(UserRole.STUDENT);
        topContainer.add(announcementManagementPanel, new ColumnData(0.3));

        panel.add(topContainer, new RowData(1, -1, new Margins(4)));

        ActivityManagementPanel activityManagementPanel = new ActivityManagementPanel(UserRole.STUDENT);
        panel.add(activityManagementPanel, new RowData(1, -1, new Margins(4)));

        panel.setTopComponent(getToolBar());
        add(panel, new FillData());

        SelectionEventBus.getInstance().registerListener(CourseChangeEvent.Update.class, new SelectionEventListener<CourseDTO>() {

            @Override
            public void selectionChanged(CourseDTO selectedObject) {
                course = selectedObject;
                setHeading(course.getBasicInfo());
            }
        });

        SelectionEventBus.getInstance().registerListener(CourseChangeEvent.TeamSelected.class, new SelectionEventListener<TeamDTO>() {

            @Override
            public void selectionChanged(TeamDTO selectedObject) {
                team = selectedObject;
            }
        });

    }

    /**
     * {@inheritDoc}
     */
    @Override
    protected void afterRender() {
        super.afterRender();
        String[] split = History.getToken().split(":");
        if (split.length == 2) {
            CourseDTO course = new CourseDTO();
            course.setPkid(Integer.valueOf(split[1]));
            GwtCourseService.Util.getInstance().getCourses(course, new AsyncCallbackAdapter<List<CourseDTO>>() {

                @Override
                public void onSuccess(List<CourseDTO> result) {
                    if (result.size() == 1) {
                        SelectionEventBus.getInstance().fireEvent(CourseChangeEvent.UPDATE, result.get(0));
                    }
                }

            });
        }
    }

    /**
     * Initializes and returns toolbar.
     * 
     * @return toolbar
     */
    public ToolBar getToolBar() {

        ToolBar toolBar = new ToolBar();

        Button updateTeamInfoButton = new Button("Update Team Info", AbstractImagePrototype.create(MEGAResources.ICONBUNDLE.updateTeam()));

        updateTeamInfoButton.addSelectionListener(new SelectionListener<ButtonEvent>() {

            @Override
            public void componentSelected(ButtonEvent ce) {
                TeamInformationEditDialog dialog = new TeamInformationEditDialog();
                dialog.update(team, new DialogCallbackAdapter<TeamDTO>() {

                    @Override
                    public void onOk(TeamDTO param) {
                        getService().updateTeam(team, new AsyncCallbackAdapter<CourseDTO>() {

                            @Override
                            public void onSuccess(CourseDTO result) {
                                SelectionEventBus.getInstance().fireEvent(CourseChangeEvent.UPDATE, result);
                            }
                        });
                    }
                });

            }
        });
        toolBar.add(updateTeamInfoButton);

        Button getReviewButton = new Button("Show Submissions To Review", AbstractImagePrototype.create(MEGAResources.ICONBUNDLE.showRevision()));

        getReviewButton.addSelectionListener(new SelectionListener<ButtonEvent>() {

            @Override
            public void componentSelected(ButtonEvent ce) {
                SubmissionReviewDialog dialog = new SubmissionReviewDialog(team.getPkid());
                dialog.show();
            }
        });

        toolBar.add(getReviewButton);

        return toolBar;

    }

    /**
     * Returns GWT course service instance.
     * 
     * @return GWT service
     */
    public static GwtCourseServiceAsync getService() {
        return GwtCourseService.Util.getInstance();
    }

}
